package com.kx.kxblog.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.kx.kxblog.bean.Group;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.util.Date;

@Data
@MappedSuperclass
@Accessors(chain = true)
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"hibernateLazyInitializer","handler"})
public class AbstractEntity {
    @Id
    @Column(nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @ApiModelProperty(value = "ID")
    @Null(groups = Group.Insert.class)
    @NotNull(groups = Group.ById.class)
    private Integer id;

    @Column(nullable = false, updatable = false)
    @CreatedDate // 自动生成创建时间
    @JsonIgnore
    @ApiModelProperty(value = "创建时间", accessMode = ApiModelProperty.AccessMode.READ_ONLY, hidden = true)
    private Date createTime;

    @Column(updatable = false)
    @CreatedBy // 自动生成创建时间
    @JsonIgnore
    @ApiModelProperty(value = "创建人id", accessMode = ApiModelProperty.AccessMode.READ_ONLY, hidden = true)
    private Integer creatorId;

    @LastModifiedDate // 自动生成更新时间
    @Column(nullable = false)
    @JsonIgnore
    @ApiModelProperty(value = "更新时间", accessMode = ApiModelProperty.AccessMode.READ_ONLY, hidden = true)
    private Date updateTime;

    @Column
    @LastModifiedBy // 自动生成创建时间
    @JsonIgnore
    @ApiModelProperty(value = "修改人id",accessMode = ApiModelProperty.AccessMode.READ_ONLY, hidden = true)
    private Integer updaterId;
}
